import req from '../utils/request';
import * as URL from '../constans/api';
// 购物车列表
export function getCartList() {
  const sum = 0;
  const total = 0;
  function getSum(arr, sum) {
    for (const item of arr) {
      sum = sum + Number(item.price);
    }
    return Math.round(sum);
  }
  function getTotal(arr, total) {
    for (const item of arr) {
      total = total + item.count;
    }
    return total;
  }
  return req({
    url: URL.CART,
    method: 'get',
  })
    .then((res) => ({
      msg: res.data.msg,
      code: res.data.code,
      totalQuantity: getTotal(res.data.result, total),
      result: res.data.result,
      totalPrice: getSum(res.data.result, sum),
    }))
    .catch((err) => Promise.reject(err.response.data));
}
//  删除购物车某个商品
export function DeleteCart(id) {
  return req({
    url: URL.CART,
    method: 'delete',
    data: {
      ids: [id],
    },
  })
    .then((res) => {
      return res.data;
    })
    .catch((err) => Promise.reject(err.response.data));
}
//  购物车选择规格
export function getGoodsSku(id) {
  return req({
    url: URL.GOODS_SKU + '/' + id,
    method: 'get',
  })
    .then((res) => {
      return res.data;
    })
    .catch((err) => Promise.reject(err.response.data));
}
